package com.yifushidai.service;

import com.alibaba.fastjson.JSONObject;
import com.yifushidai.entity.OpenRecordEntity;
import com.yifushidai.mapper.OpenRecordEntityMapper;
import com.yifushidai.utils.CalendarUtils;
import com.yifushidai.utils.DateUtils;
import com.yifushidai.utils.PageUtils;
import com.yifushidai.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Service
public class OpenService {
    @Autowired
    private OpenRecordEntityMapper openMapper;

    public R queryOpenRecords(String mac, String startTime, String endTime, int pageIndex, int pageSize, String lastdate) throws ParseException {
        List<JSONObject> openList = new ArrayList<>();
        String eTime = DateUtils.format(DateUtils.toDate2(endTime));
        String startQueryDate = getStartQueryDate(startTime, pageIndex, lastdate);
        String curDate = DateUtils.format(new Date());
        int[] evedayCount = openMapper.queryEveDaycount(mac, startTime, CalendarUtils.dayAddOrSub2(eTime, 1));
        int totalCount = evedayCount.length;
        for (int i = 0; i < pageSize; i++) {
            //判断是否可提前结束查找
            startQueryDate = DateUtils.format(DateUtils.toDate2(CalendarUtils.dayAddOrSub2(startQueryDate, 1)));//"yyyy-MM-dd"
            //System.out.println("startQueryDate 开始:"+startQueryDate);
            List<OpenRecordEntity> todayOpenList = openMapper.queryByCurday(mac, startQueryDate);// TODO 优化SQL
            //忽略空值  直到有数据  从有数据开始  分页
            if (!todayOpenList.isEmpty()) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("todayStr", startQueryDate);
                jsonObject.put("todayOpenList", todayOpenList);
                openList.add(jsonObject);
                lastdate = DateUtils.format(todayOpenList.get(0).getOpenTime(), "yyyy-MM-dd HH:mm:ss");
            } else {
                i = i - 1;
            }
            //累加到当前日 或者 超过结束日期 就不循环查询了
            if (DateUtils.toDate2(startQueryDate).getTime() >= DateUtils.toDate2(curDate).getTime()) {
                System.out.println("break:curDate" + curDate);
                break;
            }
            if (DateUtils.toDate2(startQueryDate).getTime() >= DateUtils.toDate2(eTime).getTime()) {
                System.out.println("break:endTime" + eTime);
                break;
            }
        }
        openList.sort(new Comparator<JSONObject>() {
            @Override
            public int compare(JSONObject o1, JSONObject o2) {
                return o2.getString("todayStr").compareTo(o1.getString("todayStr"));
            }
        });
        return R.ok().put(new PageUtils(openList, totalCount, pageSize, pageIndex, lastdate));
    }


    private String getStartQueryDate(String startTime, int pageIndex, String lastdate) throws ParseException {
        String sTime = DateUtils.format(DateUtils.toDate2(startTime));
        String startQueryDate;
        if (pageIndex == 1) {
            startQueryDate = CalendarUtils.dayAddOrSub2(sTime, -1);
        } else {
            startQueryDate = CalendarUtils.dayAddOrSub2(DateUtils.format(DateUtils.toDate2(lastdate)), 0);
        }
        return startQueryDate;
    }
}
